package gui;

import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Color;
import javax.swing.SwingConstants;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

import javax.swing.JComboBox;

import Entities.AssessmentEntity;
import Entities.RequestEntity;
import common.ChatIF;
import common.CommonMethods;
import javax.swing.JTextField;
/**
 * 
 * @author Chen Parodi
 * This GUI Class services the Assessors,
 * With it they are able to see the requests waiting to analyzed.
 *  *
 */
public class SignificationAssessment extends JPanel implements ChatIF{
	String pid;
	String request;
	String deadline;
	SignificationAssessment significtionAssessment;
	AcademicFrame frame;
	AssessorPanel assessor;
	Analyze analyze;
	ExtendAssessDuration extendAssessDuration;
	private JComboBox<String> assessorRequestsComboBox;
	private JButton btnAnalyze;
	private RequestEntity myEnt;
	private String myStatus;
	AssessmentEntity assessmentEntity;
	
	public SignificationAssessment(AcademicFrame frame,AssessorPanel assessor, String pid) {
		super();
		this.pid=pid;
		this.frame=frame;
		this.assessor=assessor;	
		this.significtionAssessment=this;
		frame.setSize(500, 500);
		initialize();
	}
	
	private void initialize() {
		setLayout(null);
		
		btnAnalyze = new JButton("Analyze Request");
		btnAnalyze.setEnabled(false);
		btnAnalyze.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				request=assessorRequestsComboBox.getSelectedItem().toString();

				ArrayList<Object> message= new ArrayList<Object>();
				assessmentEntity= new AssessmentEntity();
				assessmentEntity.setRid(request);
				message.add("NewAssessmentRow");
				message.add(assessmentEntity);
				
				AcademicFrame.myclient.sendMessageToServer(message);			
			}
		});
		btnAnalyze.setBounds(283, 89, 140, 23);
		add(btnAnalyze);
		
		JButton btnCancel = new JButton("Back");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				frame.setSize(500, 500);
				frame.setContentPane(assessor);
			}
		});
		btnCancel.setBounds(283, 123, 140, 23);
		add(btnCancel);
		
		JLabel lblSignificationAssessment = new JLabel("Signification Assessment");
		lblSignificationAssessment.setHorizontalAlignment(SwingConstants.LEFT);
		lblSignificationAssessment.setForeground(Color.BLACK);
		lblSignificationAssessment.setFont(new Font("Tahoma", Font.BOLD, 32));
		lblSignificationAssessment.setBounds(10, 11, 430, 56);
		add(lblSignificationAssessment);
		
		JLabel label = new JLabel("_______________________________________________________________________");
		label.setBounds(10, 53, 430, 14);
		add(label);
		
		assessorRequestsComboBox = new JComboBox<String>();
		assessorRequestsComboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {			
				if(!assessorRequestsComboBox.getSelectedItem().toString().equalsIgnoreCase("--Choose a Request--")){
					ArrayList<Object> message = new ArrayList<Object>();
					String choosenID = (String)assessorRequestsComboBox.getSelectedItem();
					String code = "trackRequest";
					message.add(code);
					message.add(choosenID);
					AcademicFrame.myclient.sendMessageToServer(message);					
					btnAnalyze.setEnabled(true);	
				}
				else{
					btnAnalyze.setEnabled(false);
				}				
			}
		});
		assessorRequestsComboBox.setBounds(84, 90, 166, 20);
		add(assessorRequestsComboBox);
		
		JLabel lblRequest = new JLabel("Request#");
		lblRequest.setBounds(22, 93, 88, 14);
		add(lblRequest);
	}
	public void display(Object message)
	{
		ArrayList<Object> listofAssessorRequests=new ArrayList<Object>();
		ArrayList<Object> TaskDuration=new ArrayList<Object>();
		ArrayList<Object> requestedData=new ArrayList<Object>();
		ArrayList<Object> extention=new ArrayList<Object>();
		listofAssessorRequests.clear(); // clear the info array list
		TaskDuration.clear(); // clear the info array list
		requestedData.clear();
		extention.clear();
		listofAssessorRequests = (ArrayList<Object>)message;
		TaskDuration = (ArrayList<Object>)message;		
		requestedData= (ArrayList<Object>)message;
		extention= (ArrayList<Object>)message;
	
		if (((String)(listofAssessorRequests.get(0))).equalsIgnoreCase("listOfExRequests"))
		{
			ArrayList<String> assessorRequests = (ArrayList<String>)message; 
									
			int numberOfSystems = Integer.parseInt((assessorRequests.get(1)).toString()); // get the number of systems in the list
			this.assessorRequestsComboBox.addItem("--Choose a Request--");
			for (int i=0; i < numberOfSystems ; i++ )
			{
				this.assessorRequestsComboBox.addItem((String)assessorRequests.get(i+2));  // add systems to the list
			}
		}	
		else if (((String)(TaskDuration.get(0))).equalsIgnoreCase("TaskDuration"))
		{
			
			ArrayList<String> replyFromServer = (ArrayList<String>)message; 
										
			if ( ((String)(replyFromServer.get(1))).equalsIgnoreCase("TaskDurationSet") )
			{
				
				(new Dialog ("The assessment duration was set, thank you.")).setVisible(true);
			
				frame.setSize(500, 500);
				frame.setContentPane(assessor);	
			}
			else
			{
				(new Dialog ("An Error occured while writing to the database.")).setVisible(true);
				
				frame.setSize(500, 500);
				frame.setContentPane(assessor);	
			}
		}
		else if (((String)(requestedData.get(0))).equalsIgnoreCase("requestData"))
		{
			myEnt = (RequestEntity)requestedData.get(1);
			myStatus=myEnt.getStatusid();
			myEnt.setStatus(myStatus);
		//	deadlinetextField.setText(myEnt.getDeadline());
			deadline=myEnt.getDeadline();
			
		}
		else if(((String)(extention.get(0))).equalsIgnoreCase("NewAssessmentRow"))
		{	
			if(((String)(requestedData.get(1))).equalsIgnoreCase("NewAssessmentRowSet"))
			{
				analyze=new Analyze(frame,assessor,pid,request,deadline,assessmentEntity);
				frame.setContentPane(analyze);
				frame.setSize(640, 540);
			}
			else if(((String)(requestedData.get(1))).equalsIgnoreCase("ExistingAssessmentRow"))
			{
				assessmentEntity= (AssessmentEntity)(requestedData.get(2));
				analyze=new Analyze(frame,assessor,pid,request,deadline,assessmentEntity);			
				frame.setContentPane(analyze);
				frame.setSize(640, 541);
			}		
			else
			{
				(new Dialog ("An Error occured while writing to the database.")).setVisible(true);
			
				frame.setSize(500, 500);
				frame.setContentPane(assessor);	
			}
		}
	}
}
